package com.d5xx.hunters;

import com.d5xx.hunters.API.Hunter;
import com.d5xx.hunters.API.HunterPlayer;
import com.d5xx.hunters.commands.HunterAdmin;
import com.d5xx.hunters.commands.Language;
import com.d5xx.hunters.events.*;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Objects;

import static com.d5xx.hunters.Variables.*;

public final class Hunters extends JavaPlugin {

    @Override
    public void onEnable() {
        // Plugin startup logic
        Functions.load(false);

        //注册事件
        PluginManager pluginManager = getServer().getPluginManager();
        pluginManager.registerEvents(new PlayerJoin(), plugin);
        pluginManager.registerEvents(new PVPSwitch(), plugin);
        pluginManager.registerEvents(new PlayerQuit(), plugin);
        pluginManager.registerEvents(new GameDeath(), plugin);
        pluginManager.registerEvents(new PlayerPickupItem(), plugin);
        pluginManager.registerEvents(new PlayerPortal(), plugin);
        pluginManager.registerEvents(new PlayerBlock(), plugin);

        // 注册命令hunteradmin
        HunterAdmin hunterAdmin = new HunterAdmin();
        PluginCommand hAdmin = getCommand("hunteradmin");
        if (hAdmin != null) {
            hAdmin.setExecutor(hunterAdmin);
            hAdmin.setTabCompleter(hunterAdmin);
        }
        // 注册命令language
        Language language = new Language();
        PluginCommand lang = getCommand("language");
        if (lang != null) {
            lang.setExecutor(language);
            lang.setTabCompleter(language);
        }
        // 注册命令hunterplayer
        com.d5xx.hunters.commands.HunterPlayer hunterplayer = new com.d5xx.hunters.commands.HunterPlayer();
        PluginCommand hplayer = getCommand("hunterplayer");
        if (hplayer != null) {
            hplayer.setExecutor(hunterplayer);
            hplayer.setTabCompleter(hunterplayer);
        }

        //自定义事件
        GameWaiting.waiting(); // 玩家等待事件
        //MapEmpty.onMapEmpty(); // 地图为空事件

        // 世界生成
        Hunter.initWorld("Loading");

    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        for (Player player : Bukkit.getOnlinePlayers()) { //遍历所有在线玩家并踢出服务器
            HunterPlayer hunterPlayer = Hunter.getHunterPlayer(player);
            player.kickPlayer(hunterPlayer.getKeyMessage("player-kick-reload"));
        }
        Hunter.saveAllPlayersSettings();
        Functions.cleanAllScoreboard();

        // 删除世界
        Hunter.deleteWorld();
    }

}
